<?php

/*
 * TODO: REPLACE, UPDATE 
 */

function  dbExecute($query=null)
{
    global $conf;
    global $cache;
    
    if(is_array($query))
    {
        $query = getQuery($query);
    }
    
    if($conf['CACHEQUERIES']){ $time1 = microtime(true); }
    $result = mysql_query($query);
    
    if($conf['CACHEQUERIES'])
    {
    	$time2 = microtime(true);
    	$cache->DBCache[] = array(
    		'query' => $query,
    		'time' => $time2-$time1,
    	);
    }
    
    
    if($error = mysql_error())
    {
    	if($conf[DEV])
    	{
    		debug ($error, false);
    		debug ($query, false);
    	}
    	
    	//TODO: write error to DB
		return;   	
    }
    
    if(gettype($result) == 'resource')
    {
        $list = array();
        while($f = mysql_fetch_assoc($result))
        {
            $list[] = $f;
        }
        return $list;
        
    }
    
    else
    {
        return $result;
    }
    
}



function getQuery($parts)
{
    $string = '';
    //select
    if(isset($parts['select']))
    {   
        $string .= 'SELECT ';
    	if(is_array($parts['select']))
        {
            $string .= implode(',', $parts['select']);
        }
        else
        {
            $string .= $parts['select'];
        }
    }
    
    //from
    if(isset($parts['from']))
    {
        $string .= ' FROM ' . $parts['from'];
    }
    
    //leftjoin
    if(isset($parts['leftJoin']))
    {
        if(is_array($parts['leftJoin']))
        {
            $left = '';
            foreach($parts['leftJoin'] as $item)
            {
                $left .= ' LEFT JOIN ' . $item;
            }
        }
        else
        {
            $left = 'LEFT JOIN ' . $parts['leftJoin'];
        }
        
        $string .= ' ' . $left;
    }
    
    
    
    //where
    if(isset($parts['where']))
    {
        $string .= ' WHERE';
        if(is_array($parts['where']))
        {
            $where = ' ';
            foreach($parts['where'] as $item)
            {
                $where .= '(' . $item . ') AND ';
            }
            
            $where = substr($where, 0, -5);
        }
        else
        {
            $where = $parts['where'];
        }
        
        $string .= ' ' . $where;
    }
    
    //group 
    if(isset($parts['group']))
    {
        
        $string .= ' GROUP BY ' . $parts['group'];
        
    }
    
    
    //order
    if(isset($parts['order']))
    {
        if(is_array($parts['order']))
        {
            $order = ' ';
            foreach($parts['order'] as $item)
            {
                $order .= $item . ',';
            }
            $order = substr($order, 0, -1);
        }
        else
        {
            $order = $parts['order'];
        }
        
        $string .= ' ORDER BY ' . $order;
    }
    
    //limit
    
    if(isset($parts['limit']))
    {
        $string .= ' LIMIT ' . $parts['limit'];
    }
    
    
    return $string;

}



function returnFirstRow($query)
{
    $result = dbExecute($query);
    if(sizeof($result))
    {
        return $result[0];
    }
    else
    {
        return false;
    }
}



function dbInsert($data, $table)
{
    $q = 'INSERT INTO ' . $table . ' (';
    //add fields
    $fields = '';
    foreach($data as $key=>$val)
    {
        $fields .= '`' . $key .'`, '; 
    }
    
    $fields = substr($fields, 0, -2);
    
    $q .= $fields . ') VALUES (';
    
    $values = '';
    foreach($data as $key=>$val)
    {
        if(is_numeric($val))
        {
            $values .= $val . ', ';
        }
        elseif($val == '__NOW()__')
        {
             $values .= 'NOW(), ';
        }
        elseif(is_string($val))
        {
            $values .= '"' . mysql_real_escape_string($val) .'", '; 
        }
        
        
    }
    
    $values = substr($values, 0, -2);
    
    $q .= $values . ')';
    
    dbExecute($q);
    return true;
}


?>